<?php

namespace App\Controller\Home;

use App\Controller\Example\RechargelExample;
use App\Models\ConfigModel;
use App\Models\Conn;
use App\Models\RechargeModel;
use App\Models\UsersModel;
use Core\Common\Common;
use Core\Enums\ErrorEnum;
use Core\Result\Result;
use Core\Utils\Utils;
use Config\Config;

class Recharge
{
    /**
     * 公司打款充值信息
     * entry  token
     * return json
     * */
    public function getComRechargeInfo($data){
        $conn = new Conn();
        $retData["bank_card"] = ConfigModel::getConfigSingleValName($conn,Config::BANK_CARD);
        $retData["bank"] = ConfigModel::getConfigSingleValName($conn,Config::BANK);
        $retData["bank_name"] = ConfigModel::getConfigSingleValName($conn,Config::BANK_NAME);
        $retData["subbranch"] = ConfigModel::getConfigSingleValName($conn,Config::SUBBRANCH);
        Result::success($retData);
    }
    /**
     * 我的充值列表
     * entry  token
     * return json
     * */
    public function rechargeList($data){
        $assemble = Common::assemble(new RechargelExample(), $data);
        $conn = new Conn();
        $assemble->uid = UsersModel::getUserInside($conn, $assemble->token)["id"];
        $rechargeListInside = RechargeModel::getRechargeListInside($conn, $assemble);
        foreach ($rechargeListInside as $item=>$value){
            $rechargeListInside[$item]["state"] = self::getRechargeState($value["state"]);
        }
        Result::success($rechargeListInside);
    }

    /**
     * 上传充值凭证
     * entry  token,serial,proof,amount,notes
     * return json
     * */
    public function addRecharge($data){
        $assemble = Common::assemble(new RechargelExample(), $data);
        if($assemble->amount <=0 || $assemble->notes == null || $assemble->proof == null){
            Result::error(ErrorEnum::PARAMETERERROR);
        }
        $conn = new Conn();
        $assemble->uid = UsersModel::getUserInside($conn, $assemble->token)["id"];
        $assemble->serial = Utils::createOrderNo();
        $conn->shiwuStart();
        if(RechargeModel::insertRecharge($conn,$assemble) != 1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::UPLOADFAILED);
        }
        $conn->shiwuEnd();
        Result::success("ok");
    }
    private static function getRechargeState($state){
        //1待审核 2审核通过 3 审核不通过 -1删除
        switch ($state){
            case 1 : return "待审核";
            case 2 : return "通过";
            case 3 : return "不通过";
            default : return "已删除";
        }
    }
}